﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MsTestUtilities.RowTests;

namespace MsTestUtilities
{
    public static class ExAssert
    {        
        public static void Throws<TException>(Action action)
            where TException : Exception
        {
            var expectation = new ExceptionExpectation<TException>();
            EvaluateExpectation(expectation, action);
        }

        public static void Throws<TException>(Action action, string message)
            where TException : Exception
        {
            var expectation = new ExceptionExpectation<TException>(message);
            EvaluateExpectation(expectation, action);
        }

        public static void Throws<TException>(Action action, Predicate<TException> predicate)
            where TException : Exception
        {
            var expectation = new ExceptionExpectation<TException>(predicate);
            EvaluateExpectation(expectation, action);
        }

        private static void EvaluateExpectation<TException>(ExceptionExpectation<TException> expectation, Action action)
            where TException : Exception
        {
            var result = expectation.IsSatisfiedFor(action);
            if (!result.AssertionHolds)
            {
                throw new AssertFailedException(result.AssertionError);
            }
        }
    }
}
